package name.wilbur.course.db.bigone.service.impl;

import java.util.Map;

import name.wilbur.course.db.bigone.dao.DAOFactory;
import name.wilbur.course.db.bigone.dao.impl.CityDAO;
import name.wilbur.course.db.bigone.dao.impl.ICityDAO;
import name.wilbur.course.db.bigone.dao.impl.IOccupationDAO;
import name.wilbur.course.db.bigone.dao.impl.OccupationDAO;
import name.wilbur.course.db.bigone.model.City;
import name.wilbur.course.db.bigone.service.IPrepareService;

public class PrepareService implements IPrepareService {
	private ICityDAO cityDAO;
	private IOccupationDAO occupationDAO;

	public PrepareService() {
	}
	
	public String[] getAllCityNames() {
		Map<String, String> cities = prepareCity();
		String []cityNames;
		if (cities == null) {
			cityNames = new String[1];
			cityNames[0] = "";
		} else {
			cityNames = cities.values().toArray(new String[1]);
		}
		return cityNames;
	}
	
	public City[] getAllCitys() {
		return getCityDAO().findAll().toArray(new City[1]);
	}
	
	public City getCityByCityName(String cityName) {
		return (City) getCityDAO().findByPK(getCityDAO().getPkByName(cityName));
	}
	
	public Map<String, String> prepareCity() {
		return getCityDAO().findCitys(null);
	}
	
	public Map<String, String> prepareOccupation() {
		return getOccupationDAO().findOccupations(null);
	}
	
	public ICityDAO getCityDAO() {
		if (cityDAO == null)
			cityDAO = (CityDAO) DAOFactory.createDAO(CityDAO.class);
		return cityDAO;
	}
	public IOccupationDAO getOccupationDAO() {
		if (occupationDAO == null)
			occupationDAO = (OccupationDAO) DAOFactory.createDAO(OccupationDAO.class);
		return occupationDAO;
	}
}
